﻿using JTParcrelHelper.MessageClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JTParcrelHelper.business
{
    public class ServerTimeTickManager
    {
        public const String TIME_TICK_KEY = "TimeTick";
        public event EventHandler<EargsTimeTick> OnServerTimeTickEvent;
        private int timeTickInterval;
        private static System.Timers.Timer mainTimer = null;
        private int timeTickTimes = 0;
        private int removeCount = 0;
        public Object mute = new Object();
        private bool hasTaskTodo = true;
        private bool deletedLog = false;
        private bool autoUploadPic = true;




        #region 单例构造
        private static ServerTimeTickManager instance;
        public static ServerTimeTickManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ServerTimeTickManager();
                }
                return instance;
            }
        }
        #endregion

        #region 定时器
        private void CreateTimer()
        {
            if (mainTimer == null)
            {
                mainTimer = new System.Timers.Timer();

                mainTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerElapsedEventHandler);
                mainTimer.Interval = 3000;
                mainTimer.AutoReset = true;
                mainTimer.Enabled = false;
            }
        }

        public void StartTimer(int mills)
        {
            if (mills < 500)
            {
                mills = 3000;
            }
            if (mainTimer != null && !mainTimer.Enabled)
            {
                mainTimer.Interval = mills;
                mainTimer.Start();
            }
        }

        public void StopTimer()
        {
            if (mainTimer != null)
            {
                mainTimer.Stop();
            }
        }

        public void DisposeTimer()
        {
            if (mainTimer != null)
            {
                mainTimer.Stop();
                mainTimer.Dispose();
                mainTimer = null;
            }
        }

        private void TimerElapsedEventHandler(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                //mainTimer.Stop();
                //DateTime dtNow = System.DateTime.Now;
                //PacketInfo.Instance.downLoadFtpFiles();
                DoTriggerTimeTickEvent(null);
                //mainTimer.Start();
            }
            catch (Exception ex)
            {

            }

        }

        private void DoTriggerTimeTickEvent(object dummyObj)
        {
            if (this.OnServerTimeTickEvent != null)
            {
                EventHandlerTrigger.TriggerEvent<EargsTimeTick>(this.OnServerTimeTickEvent, this,
                    new EargsTimeTick());
            }
        }
        #endregion

        public void Init()
        {
            this.OnServerTimeTickEvent += this.ServerTimeTickEventHandler;
            this.CreateTimer();
            this.StartTimer(timeTickInterval);

        }

        public void ReadConfiguration()
        {
            try
            {
                this.timeTickInterval = 3000;
            }
            catch (System.Exception ex)
            {
                //Logger.LogError("SortInfoManager.ReadConfiguration", "【ServerInfo】：  SortInfoManager read configuration Failed : " + ex.Message);
            }
        }

        public void ShutDown()
        {
            this.CloseAllTimer();
        }

        private void ThrowTimeTickException()
        {
            throw new NotImplementedException();
        }

        private void ServerTimeTickEventHandler(object sender, EargsTimeTick args)
        {
            try
            {
                timeTickTimes++;
                mainTimer.Stop();
                DateTime dtNow = System.DateTime.Now;

               



                mainTimer.Start();
            }
            catch (System.Exception ex)
            {
                mainTimer.Start();
                //Logger.LogError("ServerTimeTickHandler", "【Bussiness】： " + ex.Message);
            }
        }

        private void HandleUploadImgThread()
        {

        }




        /// <summary>
        /// 停止所有的定时器
        /// </summary>
        private void CloseAllTimer()
        {
            this.StopTimer();
            this.DisposeTimer();
        }
        //////////////////////////////////////////////////////////////////////////End Of Class
    }
}
